package com.example.looperlearn;

import android.os.Handler;
import android.os.LocaleList;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

public class WorkerHandler extends Handler {
    private static final String TAG = "WorkerHandler";

    public static WorkerHandler mWorkerHandler;

    private WorkerHandler(Looper looper) {
        super(looper);
        Log.d(TAG, "WorkerHandler: " + Thread.currentThread().getName());
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case 1:
                Log.d(TAG, "handleMessage:1" + Thread.currentThread().getName());
                break;
            case 2:
                Log.d(TAG, "handleMessage:2" + Thread.currentThread().getName());
                break;
            default:
                Log.d(TAG, "handleMessage: default");
        }
    }

    static class TestLooper extends Thread {

        public TestLooper() {
            super("TestLooper");
        }

        @Override
        public void run() {
            // Initialize the current thread as a looper.
            Looper.prepare();
            mWorkerHandler = new WorkerHandler(Looper.myLooper());
            Looper.loop();
        }

        public Looper getLooper() {
            // 获得当前线程的looper对象
            Log.d(TAG, "getLooper: " + Looper.myLooper().getThread().getName());
            return Looper.myLooper();
        }
    }

    public WorkerHandler getmWorkerHandler() {
        return mWorkerHandler;
    }
}
